package com.wootion.cims.module.systemsetting.dictionary.controller;

import com.alibaba.fastjson.JSONObject;
import com.wootion.cims.module.systemsetting.dictionary.model.LogAnotion;
import com.wootion.cims.module.systemsetting.dictionary.model.SysDictionary;
import com.wootion.cims.module.systemsetting.dictionary.service.SysDictionaryService;
import com.wootion.cims.response.ResponseResult;
import com.wootion.cims.response.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Optional;

/**
 * 用户管理
 * @author YaoYiChao
 */
@RestController
@RequestMapping("/sysdictionary")
@Api(value = "数据字典管理")
@RequiresAuthentication
public class SysDictionaryController {

    @Resource
    SysDictionaryService sysDictionaryService;

    @PostMapping("/save")
    @LogAnotion(type = "新增",value = "0",incident = "字典信息",prefixContent = "字典名称")
    @ApiOperation(value = "数据字典管理-新增", httpMethod = "POST", notes = "")
    public ResponseResult save(@RequestBody @Validated SysDictionary sysDictionary) {
        return sysDictionaryService.insert(sysDictionary);
    }

    @PostMapping("/update")
    @LogAnotion(type = "修改",value = "0",incident = "字典信息",prefixContent = "字典名称")
    @ApiOperation(value = "数据字典管理-修改", httpMethod = "POST", notes = "{\"id\":\"255762da-3072-4b60-898b-5bc8142250c6\",\"password\":\"123456\",\"role\":\"741\",\"tel\":\"123456\",\"remark\":\"123456\",\"communitys\":[\"741\",\"852\"],\"username\":\"741852\"}")
    public ResponseResult update(@RequestBody @Validated SysDictionary sysDictionary) {
        return sysDictionaryService.update(sysDictionary);
    }

    @PostMapping("/delete")
    @LogAnotion(type = "删除",value = "0",incident = "字典信息",prefixContent = "字典名称")
    @ApiOperation(value = "用户管理-删除", httpMethod = "POST", notes = "{\"id\":\"255762da-3072-4b60-898b-5bc8142250c6\"}")
    public ResponseResult delete(@RequestBody JSONObject jsonObject) {
        if (!Optional.ofNullable(jsonObject.getString("id")).isPresent()){
            return ResponseResult.builder().code(ResultCode.PARAM_NOT_COMPLETE.getCode()).data(ResultCode.PARAM_NOT_COMPLETE.getMessage()).build();
        }
        return sysDictionaryService.deleteById(jsonObject.getString("id"));
    }

    @PostMapping(path = "/list")
    @ApiOperation(value = "用户管理-列表查询", httpMethod = "POST", notes = "{\"page\":0,\"limit\":10,\"condititon\":\"关键字\"}")
    public ResponseResult list(@RequestParam(required = false,defaultValue = "") String condition,
                               @RequestParam(defaultValue = "1") Integer page,
                               @RequestParam(defaultValue = "18") Integer limit,
                               @RequestParam(required = false,defaultValue = "") String type) {
        return sysDictionaryService.selectPage(limit,page,condition,type);
    }
}
